package com.lgrglw.search.ke.dao;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;
import org.springframework.stereotype.Repository;

import com.lgrglw.common.spring.PropertyOper;
import com.lgrglw.search.dao.IndexBaseDao;
import com.lgrglw.search.dao.IndexReaderOper;
import com.lgrglw.search.dao.SearchContants;
import com.lgrglw.search.dao.SearchTools;
import com.lgrglw.search.ke.mapping.ChapterSearchEntity;

/**
 * 客户化章节搜索对象
 * 
 * @author liwei
 * 
 */
@Repository
public class ChapterSearchDao extends IndexBaseDao<ChapterSearchEntity, String> {

	@SuppressWarnings("deprecation")
	public Map<String, List<ChapterSearchEntity>> search(String keyword,
			String area, boolean attach, List<String> docId)throws Exception {
		Map<String, List<ChapterSearchEntity>> map = new HashMap<String, List<ChapterSearchEntity>>();
		if (docId != null && docId.size() > 0) {
			IndexReaderOper oper=new IndexReaderOper(PropertyOper.getInstance().getIndexDir()+ChapterSearchEntity.alias);
			Analyzer analyzer=new org.wltea.analyzer.lucene.IKAnalyzer();
			BooleanQuery sum = new BooleanQuery();
			QueryParser queryParser =null;
			if(attach){//是否搜索附件
				queryParser = new MultiFieldQueryParser(new String[]{"menuTitle","content","accessoryConent","keywords"},analyzer);
			}else{
				queryParser = new MultiFieldQueryParser(new String[]{"menuTitle","content","keywords"},analyzer);
			}
			queryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
	        Query keywordQuery=queryParser.parse(keyword);
			sum.add(keywordQuery,BooleanClause.Occur.MUST);
			SearchTools.buildQueryForTerm(sum, "area", area, SearchContants.MUST);
			BooleanQuery docs=new BooleanQuery();//构建知识ID过滤对象
			for(int i=0;i<docId.size();i++){
				SearchTools.buildQueryForTerm(docs, "docId", docId.get(i), SearchContants.SHOULD);
			}
			sum.add(docs,BooleanClause.Occur.MUST);
			Hits hit=oper.search(sum, null);//获取搜索结果
			//TODO 这边要注意，到时候需要检查hit是不是只读取了100条记录
			for(int i=0;i<hit.length();i++){
				Document doc=hit.doc(i);
				ChapterSearchEntity chapter=new ChapterSearchEntity();
				chapter.setDocId(doc.getField("docId").toString());//章节所属的知识ID
				chapter.setMenuId(doc.getField("menuId").toString());//章节ID
				chapter.setMenuTitle(doc.getField("menuTitle").toString());//章节标题
				//放入MAP集合中
				List<ChapterSearchEntity> list=map.get(chapter.getDocId());
				if(list==null){
					list=new LinkedList<ChapterSearchEntity>();
				}
				list.add(chapter);
				map.put(chapter.getDocId(), list);
			}
			oper.end();//关系锁文件
		}
		
		return map;
	}
}
